package io.renren.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.renren.config.MessagePropertiesConfig;
import io.renren.constant.AppConstant;
import io.renren.dao.IllegalAccidentsDao;
import io.renren.dao.IllegalCodeDao;
import io.renren.dao.IllegalVehicleDao;
import io.renren.dao.MediaFileDao;
import io.renren.dao.PoliceOnlineDao;
import io.renren.dao.RoadAccidentDao;
import io.renren.dao.ShootDao;
import io.renren.dao.ShootVehicleDao;
import io.renren.dao.UserDrivingLicenseDao;
import io.renren.entity.DrivingLicense;
import io.renren.entity.MediaFile;
import io.renren.entity.UserDrivingLicense;
import io.renren.entity.UserDrivingLicenseDto;
import io.renren.entity.UserReservation;
import io.renren.entity.Vehicle;
import io.renren.entity.VioCodewfdm;
import io.renren.entity.VioSurveil;
import io.renren.entity.VioViolationDto;
import io.renren.po.CollectionTimeInfo;
import io.renren.po.IllegalCode;
import io.renren.po.IllegalCrossingInfo;
import io.renren.po.IllegalVehicle;
import io.renren.po.IllegalVehicleRanking;
import io.renren.po.IllegalVehicleResp;
import io.renren.po.RoadAccidentInfo;
import io.renren.po.Shoot;
import io.renren.po.ShootVehicle;
import io.renren.po.UserVehiclePO;
import io.renren.po.UserVehiclePODto;
import io.renren.service.DataInfoQueryService;
import io.renren.service.PoliceService;
import io.renren.util.SerialUtil;
import io.renren.vo.IllegalCrossingInfoHistryList;
import io.renren.vo.IllegalCrossingInfoList;
import io.renren.vo.IllegalCrossingInfoRes;
import io.renren.vo.IllegalHistoryRedLightRes;
import io.renren.vo.IllegalHistoryRoadRes;
import io.renren.vo.IllegalVehicleHistryRes;
import io.renren.vo.IllegalVehicleList;
import io.renren.vo.IllegalVehicleReq;
import io.renren.vo.Pager;
import io.renren.vo.RespModel;
import io.renren.vo.RoadAccidentInfoList;
import io.renren.vo.ShootReq;
import io.renren.vo.ShootRes;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 交管业务
 *
 * @author Administrator
 */
@Service
public class PoliceServiceImpl implements PoliceService {
    private static final Logger logger = LoggerFactory.getLogger(PoliceServiceImpl.class);

    @Autowired
    private IllegalCodeDao illegalCodeDao;
    @Autowired
    private ShootDao shootDao;
    @Autowired
    private MediaFileDao mediaFileDao;
    @Autowired
    private IllegalAccidentsDao illegalAccidentsDao;
    @Autowired
    private IllegalVehicleDao illegalVehicleDao;
    @Autowired
    private RoadAccidentDao roadAccidentDao;
    @Autowired
    private ShootVehicleDao shootVehicleMapper;

    @Autowired
    private MessagePropertiesConfig propertiesConfig;

    @Autowired
    private PoliceOnlineDao policeOnlineDao;
    @Autowired
    private UserDrivingLicenseDao userDrivingLicenseDao;
    @Autowired
    private DataInfoQueryService dataInfoQueryService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public List<IllegalCode> queryillegalInfo(String inputText) {
        logger.info("----根据输入信息查询违法细则-----" + inputText);
        return illegalCodeDao.queryillegalInfo(inputText);

    }

    @Override
    public int createShoot(ShootReq shootReq) {
        try {
            String shootId = SerialUtil.createNewOrderID();
            shootReq.setShootId(shootId);
            shootDao.createShoot(shootReq);

            List<Long> list = shootReq.getList();
            for (Long mediaId : list) {
                MediaFile mediaFile = new MediaFile();
                mediaFile.setMediaId(mediaId);
                mediaFile.setShootId(shootId);
                mediaFileDao.updateMediaFile(mediaFile);
            }

//            String url = propertiesConfig.getSYS_HCT6TO1_URL();
//            Map<String, Object> user = new HashMap<String, Object>();
//            user.put("hphm", shootReq.getHphm());
//            user.put("hpzl", shootReq.getHpzl());
//            String resultStr = new HystrixPostUtil(url, user).execute();
//            RespModel respModel = JSON.parseObject(resultStr, RespModel.class);
//            logger.info("查询车辆电话号码.............." + resultStr);
//            if (!StringUtils.equals(respModel.getStatus(), "SUCCESS")) {
//                return 0;
//            }
//            List<ShootVehicle> listv = JSON.parseArray(respModel.getResult().toString(), ShootVehicle.class);
//            logger.info("解析车辆电话号码.............." + respModel.getResult().toString());
//            if (listv.size() == 0) {
//                return 0;
//            }

            Vehicle vehicle = new Vehicle();
            vehicle.setHPHM(shootReq.getHphm());
            vehicle.setHPZL(shootReq.getHpzl());
            List<Vehicle> vehicleInfo = dataInfoQueryService.getVehicleInfoByParams(shootReq.getHpzl(), shootReq.getHphm(), null);
            if (vehicleInfo == null || vehicleInfo.size() == 0 || StringUtils.isBlank(vehicleInfo.get(0).getSJHM())) {
                return 0;
            }
            ShootVehicle shootVehicle = new ShootVehicle();
            shootVehicle.setHphm(shootReq.getHphm());
            shootVehicle.setHpzl(shootReq.getHpzl());
            shootVehicle.setShootId(shootId);
            shootVehicle.setSjhm(vehicleInfo.get(0).getSJHM());
            shootVehicleMapper.insert(shootVehicle);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Pager<ShootRes> queryShootList(String imageType, int currentpage, int limit) {
        Page<Shoot> page = new Page<>(currentpage, limit, "CREATE_TIME", false);
        EntityWrapper<Shoot> wrapper = new EntityWrapper<>();
        wrapper.eq("SHOOT_TYPE", imageType);
        Wrapper fillWrapper = SqlHelper.fillWrapper(page, wrapper);
        List<Shoot> list = shootDao.selectPage(page, fillWrapper);
        List<ShootRes> shootResList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ShootRes shootRes = new ShootRes();
            String shootId = list.get(i).getShootId();
            String shootType = list.get(i).getShootType();
            String userName = list.get(i).getUserName();
            String shootAddress = list.get(i).getShootAddress();
            double locationX = list.get(i).getLocationX();
            double locationY = list.get(i).getLocationY();
            shootRes.setShootId(shootId);
            shootRes.setShootType(shootType);
            shootRes.setUserName(userName);
            shootRes.setShootAddress(shootAddress);
            shootRes.setLocationX(locationX);
            shootRes.setLocationY(locationY);
            List<MediaFile> mediaList = mediaFileDao.getList(shootId);
            shootRes.setList(mediaList);
            shootResList.add(shootRes);
        }
        Pager<ShootRes> pager = new Pager<>(page.getTotal(), shootResList);
        return pager;
    }

    @Override
    public IllegalVehicleList queryIllegalVehicle(String illegalType) {
        List<IllegalVehicleRanking> list = illegalVehicleDao.queryIllegalVehicle(illegalType);
        IllegalVehicleList ill = new IllegalVehicleList();
        ill.setIllegalVehicleList(list);
        return ill;
    }

    @Override
    public IllegalVehicleHistryRes queryIllegalVehicleHistry(IllegalVehicleReq illegalVehicleReq) {
        Date date = new Date(illegalVehicleReq.getCollectionTime());
        IllegalVehicle iv = new IllegalVehicle();
        iv.setIllegalType(illegalVehicleReq.getIllegalType());
        iv.setCollectionTime(date);
        List<CollectionTimeInfo> list = illegalVehicleDao.queryIllegalVehicleHistry(iv);
        IllegalVehicleHistryRes ivr = new IllegalVehicleHistryRes();
        ivr.setCollectionTimeList(list);
        if (("10391&10392").equals(illegalVehicleReq.getIllegalType()) || ("10390").equals(illegalVehicleReq.getIllegalType()) || ("10393").equals(illegalVehicleReq.getIllegalType())) {
            ivr.setTitle(propertiesConfig.getSTOP_VEHICLE_TITLE());
        } else if (("13451").equals(illegalVehicleReq.getIllegalType()) || ("12081").equals(illegalVehicleReq.getIllegalType()) || ("12431").equals(illegalVehicleReq.getIllegalType())) {
            ivr.setTitle(propertiesConfig.getGASSER_VEHICLE_TITLE());
        } else {
            ivr.setTitle(propertiesConfig.getILLEGAL_RED_VEHICLE_TITLE());
        }
        return ivr;
    }

    @Override
    public IllegalVehicleList queryIllegalVehicleHistryInfo(IllegalVehicleReq illegalVehicleReq) {
        Date date = new Date(illegalVehicleReq.getCollectionTime());
        IllegalVehicle iv = new IllegalVehicle();
        iv.setIllegalType(illegalVehicleReq.getIllegalType());
        iv.setCollectionTime(date);
        List<IllegalVehicleRanking> list = illegalVehicleDao.queryIllegalVehicleHistryInfo(iv);
        IllegalVehicleList ill = new IllegalVehicleList();
        ill.setIllegalVehicleList(list);
        return ill;
    }

    @Override
    public IllegalVehicleResp queryIllegalVehicleInfo(int id) {
        IllegalVehicleResp illVeh = illegalVehicleDao.queryIllegalVehicleInfo(id);
        return illVeh;
    }

    @Override
    public IllegalCrossingInfoList queryIllegalCrossing() {
        List<IllegalCrossingInfoRes> illegalCrossingList = illegalAccidentsDao.queryRedLightAccidents();
        IllegalCrossingInfoList ili = new IllegalCrossingInfoList();
        ili.setIllegalCrossingList(illegalCrossingList);
        return ili;
    }

    @Override
    public IllegalHistoryRedLightRes queryIllegalHistry(long collectionTime) {
        Date date = new Date(collectionTime);
        List<CollectionTimeInfo> collectionTimeInfoList = illegalAccidentsDao.queryHistoryRedLightAccidents(date);
        IllegalHistoryRedLightRes illegalHistoryRedLightRes = new IllegalHistoryRedLightRes();
        illegalHistoryRedLightRes.setCollectionTimeList(collectionTimeInfoList);
        illegalHistoryRedLightRes.setTitle(propertiesConfig.getILLEGA_INFO());
        return illegalHistoryRedLightRes;
    }

    @Override
    public IllegalCrossingInfoHistryList queryIllegalHistryInfo(long collectionTime) {
        Date date = new Date(collectionTime);
        List<IllegalCrossingInfo> list = illegalAccidentsDao.queryRedLightAccidentsHis(date);
        IllegalCrossingInfoHistryList ili = new IllegalCrossingInfoHistryList();
        ili.setIllegalCrossingList(list);
        return ili;
    }

    @Override
    public IllegalCrossingInfo queryIllegalInfo(int id) {
        IllegalCrossingInfo illegalCrossingInfos = illegalAccidentsDao.queryIllegalInfoById(id);
        return illegalCrossingInfos;
    }

    @Override
    public RoadAccidentInfoList queryRoadAccidents() {
        List<RoadAccidentInfo> roadAccidentsInfoList = roadAccidentDao.queryRoadAccidents();
        RoadAccidentInfoList rai = new RoadAccidentInfoList();
        rai.setRoadAccidentsList(roadAccidentsInfoList);
        return rai;
    }

    @Override
    public IllegalHistoryRoadRes queryAccidentsHistry(long collectionTime) {
        Date date = new Date(collectionTime);
        List<CollectionTimeInfo> collectionTimeList = roadAccidentDao.queryHistoryRoadAccidents(date);
        IllegalHistoryRoadRes illegalHistoryRoadRes = new IllegalHistoryRoadRes();
        illegalHistoryRoadRes.setCollectionTimeList(collectionTimeList);
        illegalHistoryRoadRes.setTitle(propertiesConfig.getROAD_ACCIDENTS());
        return illegalHistoryRoadRes;
    }

    @Override
    public RoadAccidentInfoList queryAccidentsHistryInfo(long collectionTime) {
        Date date = new Date(collectionTime);
        List<RoadAccidentInfo> list = roadAccidentDao.queryRoadAccidentsHistryInfo(date);
        RoadAccidentInfoList rai = new RoadAccidentInfoList();
        rai.setRoadAccidentsList(list);
        return rai;
    }

    @Override
    public RoadAccidentInfo queryRoadAccidentsInfo(int id) {
        RoadAccidentInfo roadAccidentInfos = roadAccidentDao.queryRoadAccidentById(id);
        return roadAccidentInfos;
    }

    /**
     * 查询用户车辆信息
     *
     * @param userid
     * @return
     */
    public List<UserVehiclePO> getVehicleList(String userid) {
        List<UserVehiclePO> vehicleList = policeOnlineDao.getVehicleList(userid);
        //TODO 如果存在机动车信息查询违法数据，接口限制目前不做
        return vehicleList;
    }

    /**
     * 查询用户车辆信息
     *
     * @param userid
     * @return
     */
    public List<UserVehiclePODto> getAppVehicleList(String userid) {
        List<UserVehiclePO> vehicleList = policeOnlineDao.getVehicleList(userid);
        if (vehicleList == null || vehicleList.size() == 0) {
            return new ArrayList<>();
        }
        // 查询信息
        List<UserVehiclePODto> vehiclePODtoList = new ArrayList<>();
        for (UserVehiclePO uv : vehicleList) {
            // 查询车辆基本信息，看是否存在
            Vehicle v = new Vehicle();
            v.setHPHM(uv.getLicense_plate());
            v.setFDJH(uv.getEngine_no());

            // 基本信息
            UserVehiclePODto dto = new UserVehiclePODto();
            dto.setCarNo(uv.getLicense_plate());
            dto.setCarType(uv.getPlate_type());
            dto.setFdjh(uv.getEngine_no());

            List<Vehicle> list = dataInfoQueryService.getVehicleInfoByParams(
                    uv.getPlate_type(), uv.getLicense_plate(), uv.getEngine_no());
            if (list != null && list.size() > 0) {
                v = list.get(0);
                dto.setName(v.getSYR());
                dto.setStatus(v.getZT());
                if (v.getYXQZ() != null) {
                    dto.setValidDate(sdf.format(v.getYXQZ()));
                }
//                dto.setYyStatus();
            }
            vehiclePODtoList.add(dto);
        }

        //TODO 如果存在机动车信息查询违法数据，接口限制目前不做
        return vehiclePODtoList;
    }

    /**
     * 用户绑定车辆
     *
     * @param userVehiclePO
     * @return
     */
    public RespModel saveReservationVehicle(UserVehiclePO userVehiclePO) {
        //TODO 查询6合1接口，查询机动车号牌与发动机号，判断用户输入的是否合法，合法则保存数据返回插入结果
//        if(查询6合1号牌信息为空 或者 号牌对应的发动机号与输入的不一致){
//            return new RespModel(AppConstant.STATU_FAIL.getName(), AppConstant.STATU_FAIL.getValue(), "输入的信息有误", null);
//        }
        List<UserVehiclePO> vehicleList = getVehicleList(userVehiclePO.getUserid());
        List<UserVehiclePO> collect = vehicleList.stream().filter(item ->
                StringUtils.equals(item.getEngine_no(), userVehiclePO.getEngine_no())
        ).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            // 查询车辆基本信息，看是否存在
            List<Vehicle> list = dataInfoQueryService.getVehicleInfoByParams(
                    userVehiclePO.getPlate_type(), userVehiclePO.getLicense_plate(), userVehiclePO.getEngine_no());
            if (list == null || list.size() == 0) {
                // 车辆信息不存在
                return new RespModel<>("failed", "401", "VEHICLE IS NOT EXIST", null);
            }
            policeOnlineDao.saveReservationVehicle(userVehiclePO);
            return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", null);
        } else {
            return new RespModel(AppConstant.THINGS_HAVE_BEAN_DONE.getName(), AppConstant.THINGS_HAVE_BEAN_DONE.getValue(), "用户已经绑定该车辆信息", null);
        }
    }

    /**
     * 查询用户信誉积分 预约订单 车辆有效期
     *
     * @param userVehiclePO
     * @return
     */
    public RespModel<Map> getReservationBasicInfo(UserVehiclePO userVehiclePO) {
        Map result_map = new HashMap();
        //查询爽约记录
        List<Map> breakAppointment_List = policeOnlineDao.breakAppointment(userVehiclePO.getUserid());
        if (!CollectionUtils.isEmpty(breakAppointment_List)) {
            ArrayList<Object> id_List = new ArrayList<>();
            breakAppointment_List.forEach(map ->
                    id_List.add(map.get("id_"))
            );
            //更新到场状态为爽约
            policeOnlineDao.updateBreakAppointment(id_List);
            //减少用户信誉积分
            Map<String, Object> map = new HashedMap();
            map.put("score", id_List.size() * (-10));
            map.put("userid", userVehiclePO.getUserid());
            policeOnlineDao.updateUserIntegral(map);
        }
        //查询积分
        Map userIntegraInfo_Map = policeOnlineDao.queryUserIntegraInfo(userVehiclePO.getUserid());
        if (CollectionUtils.isEmpty(userIntegraInfo_Map)) {
            result_map.put("integral", "0");
        } else {
            result_map.put("integral", String.valueOf(userIntegraInfo_Map.get("integral")));
        }

        //查询预约次数
        Integer reservationNum = policeOnlineDao.getReservationNum(userVehiclePO.getLicense_plate());
        result_map.put("reservationNum", reservationNum);
        //查询车辆有效期
        // TODO 根据6合1查询车辆有效期
        result_map.put("yxqz", "");

        return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", result_map);
    }


    public List<Map> getCheckSiteList() {
        return policeOnlineDao.getCheckSiteList();
    }

    public List<Map> queryCheckSiteServiceProject(String company_id) {
        return policeOnlineDao.queryCheckSiteServiceProject(company_id);
    }

    /**
     * 查询用户可预约时间列表
     *
     * @param userReservation
     * @return
     */
    public RespModel getReservationTimeList(UserReservation userReservation) {
        Instant instant = userReservation.getReservation_date().toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        //查询可预约时间列表
        Map paramMap = new HashMap();
        paramMap.put("company_id", userReservation.getCompany_id());
        LocalDateTime start_localtime = localDateTime.withYear(localDateTime.getYear())
                .withMonth(localDateTime.getMonthValue())
                .withDayOfYear(localDateTime.getDayOfYear())
                .withHour(LocalTime.now().getHour())
                .withMinute(localDateTime.now().getMinute())
                .withSecond(localDateTime.now().getSecond());
        paramMap.put("starttime", start_localtime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        paramMap.put("endtime", localDateTime.plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        paramMap.put("state", '2');
        List<Map> reservationTimeList = policeOnlineDao.queryReservationTimeList(paramMap);
        //如果无可预约时间列表，尝试插入
        if (CollectionUtils.isEmpty(reservationTimeList)) {
            //查询可预约时间列表
            paramMap.put("state", "1");
            paramMap.put("starttime", start_localtime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            reservationTimeList = policeOnlineDao.queryReservationTimeList(paramMap);
            if (CollectionUtils.isEmpty(reservationTimeList)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                paramMap.put("reservation_date", sdf.format(userReservation.getReservation_date()));
                policeOnlineDao.saveReservationTime(paramMap);
            }
        }
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", reservationTimeList);
    }

    /**
     * 提交预约
     *
     * @param userReservation
     * @return
     */
    public RespModel submitReservationTime(UserReservation userReservation) {
        //查询该时间段是否已经预约过
        Integer timeCount = policeOnlineDao.queryReservationTimeById(userReservation.getReservation_id());
        if (timeCount > 0) {
            return new RespModel(AppConstant.STATU_FAIL.getName(),
                    AppConstant.STATU_FAIL.getValue(),
                    "该时间段已经有预约，请重新能选择", null);
        }

        //查询该用户是否已经预约过
        Integer reservationRecord_count = policeOnlineDao.queryUserReservationRecord(userReservation);
        if (reservationRecord_count > 0) {
            return new RespModel(AppConstant.STATU_FAIL.getName(),
                    AppConstant.STATU_FAIL.getValue(),
                    "已经预约过，不能再预约", null);
        }

        policeOnlineDao.updateReservationTime(userReservation);
        return new RespModel(AppConstant.STATU_SUCCESS.getName(),
                AppConstant.STATU_SUCCESS.getValue(), "", null);
    }

    /**
     * 查询车辆预约记录
     *
     * @param license_plate
     * @return
     */
    public List<Map> queryReservationRecord(String license_plate, String userid) {
        return policeOnlineDao.queryReservationRecord(license_plate, userid);
    }

    /**
     * 用户取消预约
     *
     * @param userReservation
     * @return
     */
    public RespModel cancelReservationTime(UserReservation userReservation) {
        //保存预约记录
        Integer save_count = policeOnlineDao.saveCancelReservationTime(userReservation);
        if (save_count == null && save_count == 0) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), AppConstant.STATU_FAIL.getValue(),
                    "没有预约记录，无法取消", null);
        }
        //更新预约时间段记录为可预约
        policeOnlineDao.updateCancelReservationTime(userReservation);
        //更新用户积分，取消一次，减少10个积分
        Map<String, Object> paramMap = new HashedMap();
        paramMap.put("score", -10);
        paramMap.put("userid", userReservation.getUserid());
        policeOnlineDao.updateUserIntegral(paramMap);
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(),
                "", null);
    }

    /**
     * 更新用户到场状态
     *
     * @param userReservation
     */
    public Integer updateReservationArrive(UserReservation userReservation) {
        return policeOnlineDao.updateReservationArrive(userReservation);
    }

    @Override
    public Integer deleteUserVehicle(String userid, String license_plate) {
        return policeOnlineDao.deleteUserVehicle(userid, license_plate);
    }

    /**
     * 获取驾驶证信息
     *
     * @param userid
     * @return
     */
    @Override
    public RespModel getDrivingLicense(String userid) {
        UserDrivingLicense udl = new UserDrivingLicense();
        udl.setUsername(userid);
        udl = userDrivingLicenseDao.selectOne(udl);
        if (udl == null) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), AppConstant.STATU_FAIL.getValue(), "USER DRIVINGLICENSE INFO NOT EXIST.", null);
        }
        // 组织数据
        UserDrivingLicenseDto dto = new UserDrivingLicenseDto();
        dto.setSfzmhm(udl.getSfzmhm());
        dto.setDabh(udl.getDabh());
        dto.setXm(udl.getXm());
        // 查询信息
        DrivingLicense drivingLicense = new DrivingLicense();
        drivingLicense.setDABH(udl.getDabh());
        List<DrivingLicense> licenseList = dataInfoQueryService.getDrivingLicenseInfo(drivingLicense);
        if (licenseList != null && licenseList.size() > 0) {
            DrivingLicense d = licenseList.get(0);
            dto.setZjcx(d.getZJCX());
            dto.setZt(d.getZT());
            if (d.getLJJF() != null) {
                dto.setJf(String.valueOf(d.getLJJF()));
            }
            if (d.getYXQZ() != null) {
                dto.setValidData(sdf.format(d.getYXQZ()));
            }
            if (d.getSYYXQZ() != null) {
                dto.setCheckData(sdf.format(d.getSYYXQZ()));
            }
        }
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", dto);
    }

    /**
     * 删除驾驶证信息
     *
     * @param userid
     * @return
     */
    @Override
    public Integer deleteUserDrivingLicense(String userid) {
        UserDrivingLicense userDrivingLicense = new UserDrivingLicense();
        userDrivingLicense.setUsername(userid);
        return userDrivingLicenseDao.delete(new EntityWrapper<>(userDrivingLicense));
    }

    /**
     * 保存驾驶证信息
     *
     * @param userDrivingLicense 驾驶证信息
     * @return
     */
    @Override
    public RespModel saveUserDrivingLicense(UserDrivingLicense userDrivingLicense) {
        UserDrivingLicense udl = new UserDrivingLicense();
        udl.setSfzmhm(userDrivingLicense.getSfzmhm());
        udl = userDrivingLicenseDao.selectOne(udl);
        if (udl != null) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), "401", "USER DRIVINGLICENSE INFO IS EXIST.", null);
        }
        udl = new UserDrivingLicense();
        udl.setUsername(userDrivingLicense.getUsername());
        udl = userDrivingLicenseDao.selectOne(udl);
        if (udl != null) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), "401", "USER DRIVINGLICENSE INFO IS EXIST.", null);
        }
        DrivingLicense drivingLicense = new DrivingLicense();
        drivingLicense.setXM(userDrivingLicense.getXm());// 姓名
        drivingLicense.setDABH(userDrivingLicense.getDabh());// 档案编号
        drivingLicense.setSFZMHM(userDrivingLicense.getSfzmhm());// 身份证明号码
        List<DrivingLicense> licenseInfo = dataInfoQueryService.getDrivingLicenseInfo(drivingLicense);
        if (licenseInfo == null || licenseInfo.size() == 0) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), "402", "BASE USER DRIVINGLICENSE INFO IS NOT EXIST.", null);
        }
        userDrivingLicenseDao.insert(userDrivingLicense);
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", null);
    }

    /**
     * 根据号牌号码查询违法信息
     *
     * @param vehicle 车辆信息
     * @return
     */
    @Override
    public RespModel getVioViolationList(Vehicle vehicle) {
        String hphm = vehicle.getHPHM();
        List<Vehicle> info = dataInfoQueryService.getVehicleInfoByParams(
                vehicle.getHPZL(), vehicle.getHPHM(), vehicle.getFDJH());
        if (info == null || info.size() == 0) {
            return new RespModel("fail", "401", "VEHICLE INFO IS NOT EXIST", null);
        }
        VioSurveil vioSurveil = new VioSurveil();
        vioSurveil.setHPHM(hphm);
        vioSurveil.setHPZL(vehicle.getHPZL());
        List<VioSurveil> vioSurveilList = dataInfoQueryService.getVioSurveilInfo(vioSurveil);
        if (vioSurveilList == null || vioSurveilList.size() == 0) {
            return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, null, null);
        }
        List<VioViolationDto> dtoList = new ArrayList<>();
        for (VioSurveil violation : vioSurveilList) {
            VioViolationDto vioViolationDto = new VioViolationDto();
            if (violation.getLRSJ() != null) {
                vioViolationDto.setDate(sdf.format(violation.getLRSJ()));
            }
            if (violation.getWFDZ() != null) {
                vioViolationDto.setDd(violation.getWFDZ());
            }
            VioCodewfdm vioCodewfdm = new VioCodewfdm();
            vioCodewfdm.setWFXW(violation.getWFXW());
            List<VioCodewfdm> codewfdmList = dataInfoQueryService.getVioCodewfdmInfo(vioCodewfdm);
            if (codewfdmList != null && codewfdmList.size() > 0) {
                vioCodewfdm = codewfdmList.get(0);
                vioViolationDto.setNr(vioCodewfdm.getWFNR());
                vioViolationDto.setKf(String.valueOf(vioCodewfdm.getWFJFS()));
            }
            if (violation.getFKJE() != null) {
                vioViolationDto.setFk(String.valueOf(violation.getFKJE()));
            }
            vioViolationDto.setStatus("违法未处理");
            if ("1".equals(violation.getCLBJ()) && "1".equals(violation.getJKBJ())) {
                vioViolationDto.setStatus("违法已处理");
            }

            dtoList.add(vioViolationDto);
        }
        return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, null, dtoList);
    }

    /**
     * 获取其他人的驾驶证信息
     *
     * @param drivingLicenseQuery
     * @return
     */
    @Override
    public RespModel<UserDrivingLicense> getOthersDrivingLicense(DrivingLicense drivingLicenseQuery) {
//        UserDrivingLicense udl = new UserDrivingLicense();
//        udl.setUsername(userid);
//        udl = userDrivingLicenseDao.selectOne(udl);
//        if (udl == null) {
//            return new RespModel(AppConstant.STATU_FAIL.getName(), AppConstant.STATU_FAIL.getValue(), "USER DRIVINGLICENSE INFO NOT EXIST.", null);
//        }
        DrivingLicense drivingLicenseQ = new DrivingLicense();
        drivingLicenseQ.setSFZMHM(drivingLicenseQuery.getSFZMHM());
        drivingLicenseQ.setDABH(drivingLicenseQuery.getDABH());
        List<DrivingLicense> drivingLicenseList = dataInfoQueryService.getDrivingLicenseInfo(drivingLicenseQ);
        if (drivingLicenseList == null || drivingLicenseList.size() == 0) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), "401", "USER DRIVINGLICENSE INFO NOT EXIST.", null);
        }
        DrivingLicense udl = drivingLicenseList.get(0);

        // 组织数据
        UserDrivingLicenseDto dto = new UserDrivingLicenseDto();
        dto.setSfzmhm(udl.getSFZMHM());
        dto.setDabh(udl.getDABH());
        dto.setXm(udl.getXM());
        // 查询信息
        DrivingLicense drivingLicense = new DrivingLicense();
        drivingLicense.setDABH(udl.getDABH());
        List<DrivingLicense> licenseList = dataInfoQueryService.getDrivingLicenseInfo(drivingLicense);
        if (licenseList != null && licenseList.size() > 0) {
            DrivingLicense d = licenseList.get(0);
            dto.setZjcx(d.getZJCX());
            dto.setZt(d.getZT());
            if (d.getLJJF() != null) {
                dto.setJf(String.valueOf(d.getLJJF()));
            }
            if (d.getYXQZ() != null) {
                dto.setValidData(sdf.format(d.getYXQZ()));
            }
            if (d.getSYYXQZ() != null) {
                dto.setCheckData(sdf.format(d.getSYYXQZ()));
            }
        }
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", dto);
    }

    /**
     * 获取其他人的机动车信息
     *
     * @param vehicle
     * @return
     */
    @Override
    public RespModel<Vehicle> getOthersVehicle(Vehicle vehicle) {
        List<Vehicle> vehicleList = dataInfoQueryService.getVehicleInfoByParams(
                vehicle.getHPZL(),vehicle.getHPHM(),vehicle.getFDJH());
        if (vehicleList == null || vehicleList.size() == 0) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), "401", "BASE INFO NOT EXIST.", null);
        }
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", vehicleList.get(0));
    }
}
